<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ include file="/WEB-INF/decorators/taglibs.jsp"%>
<div class="w1a">
<div class="w2">資料維護 » 常用案由  </div>
	<form:form modelAttribute="searchForm" id="searchForm" class="form">
	</form:form><br/> 
	<table id="dg" class="easyui-datagrid" style="height:600px;width:800px;" url="${appPath}/codeCause/list.json"  
		striped="true" title="常用案由"
		data-options="
		singleSelect: true,
        toolbar: '#tb',
        method: 'post',
        onBeforeLoad: onBeforeLoad,
        onClickCell: onClickCell,
        onBeforeEdit: onBeforeEdit,
        onAfterEdit: onAfterEdit,
        onCancelEdit: onCancelEdit,
        onLoadSuccess: onLoadSuccess,
		">
		<thead><tr>
	        <th field="name" width="500" editor="text">名稱</th>
	        <th field="action" width="120" formatter="action">作業</th>  
		</tr></thead>   
	</table> 
   
	<div id="tb" style="height: auto">   
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="endEditing()">存檔</a>
	</div>
</div>
<script>
function doSearch(){ 
	var object = {};
	$.each($('#searchForm').serializeArray(), function(i, v) { object[v.name] = v.value; });
	$('#dg').datagrid('load', object); 
}    

var editIndex = undefined; // 目前編輯的row index
function endEditing() {
	if (editIndex == undefined) {
		return true;
	}
	if ($("#dg").datagrid('validateRow', editIndex)) {
		$('#dg').datagrid('endEdit', editIndex);
		$('#dg').datagrid('cancelEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		/*取消編輯狀態*/
		var ed = $("#dg").datagrid("getEditor", {index:editIndex,field:"name"});
		if($(ed.target).text('getValue')==""){
			$('#dg').datagrid('endEdit', editIndex);
			$('#dg').datagrid('cancelEdit', editIndex);
			return true;
		}
		return false;
	}
}
function onBeforeLoad(param){
	$.each($('#searchForm').serializeArray(), function(i, v) { param[v.name] = v.value;});
}
function onClickCell(index, field, value){
	if(field!='action'){
		if (endEditing()) {
			$("#dg").datagrid("selectRow", index).datagrid('beginEdit',index);
			editIndex = index;
		} else {
			$("#dg").datagrid("selectRow", editIndex);
		}
	}
}
function onBeforeEdit(index, row, changes) {
	row.editing = true;
    $("#dg").datagrid("refreshRow", index);
}
function onAfterEdit(index, row, changes) {
		row.editing = false;
	    $("#dg").datagrid("refreshRow", index);
	if (Object.keys(changes).length > 0) { 
	    update(index);
	}
}
function onCancelEdit(index, row, changes) {
	row.editing = false;
    $("#dg").datagrid("refreshRow", index);
}
function appendRow(){
	/* 初始提供一空白行供新增 */
	$("#dg").datagrid("appendRow", {
		name : "",
	});
}
function onLoadSuccess(data) {
	appendRow();
	if (editIndex != undefined) { /* 如果為繼續，於reload後將該行設為editable */
		$("#dg").datagrid('selectRow', editIndex).datagrid('beginEdit',
				editIndex);
	}
}
function update(index) {
	var o = $("#dg").datagrid("getData").rows[index];
	$.post("${appPath}/codeCause/update.json", o,
		function(data) {
			if (data=='false') {
				alert("更新失敗!");
			} else{
				$("#dg").datagrid("refreshRow", index);
				if(o.code==undefined){
					$('#dg').datagrid('updateRow', {
						index : index,
						row: {
							code: data.code
						}
					});
					appendRow();
				}
			}
	});
}
function getRowIndex(target){
    var tr = $(target).closest("tr.datagrid-row");
    return parseInt(tr.attr("datagrid-row-index"));
}
function cancel(target){
    $("#dg").datagrid("cancelEdit", getRowIndex(target));
    editIndex = undefined;
}
function del(target){
    $.messager.confirm("Confirm","確定刪除?",function(r){
        if (r){
        	var index = getRowIndex(target);
        	var o = $("#dg").datagrid("getData").rows[index];
            $.post("${appPath}/codeCause/delete/"+o.code+".json", 
       			function(data) {
       				if (eval(data)) {
       					$("#dg").datagrid("deleteRow", index);
       					if(editIndex>index){
       						editIndex-=1;
       					}
       				} else {
       					alert("刪除失敗!");
       				}
       				if(editIndex==index){
       					editIndex = undefined;
       				}
       		});
        }
    }); 
}
function action(val, row, ix) {var str = "";
	var o = $("#dg").datagrid("getData").rows[ix];
	if(o.code!=undefined){
		str = str.concat("[<a href='#do' onclick=\"del(this);\" >刪除</a>]");
	}
	if (row.editing){
		str = str.concat("[<a href='#do' onclick=\"cancel(this);\" >取消</a>]");
	}
	return str; 
}
</script>